﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using LitJson;
using System.IO;


public class savedecktojson : MonoBehaviour
{
    [SerializeField]
    InputField input;

    private cardcontrol cardinfo;//取得整个组卡界面卡的信息
    public List<cardtosave1> cardtosave1 = new List<cardtosave1>();
    public List<string> savedcardname = new List<string>();
    public List<int> savedcardamount = new List<int>();
    //public carddata savedcardamount1;
    private string path;

    private GameObject Menublack;
    private GameObject Savecardnametext;
    private GameObject Savecardname;
    private GameObject Savecardmenubtn;

    JsonData savedcardtest;
    //private bool savedcardamount1;

    void cardlisttosave()
    {
        for (int i = 0; i < cardinfo.slots.Count; i++)//遍历整个卡槽
        {
            if (cardinfo.slots[i].transform.childCount > 0)//用卡槽的子物体个数来判断该卡槽是不是为空
            {
                carddata savedcardamount1 = cardinfo.slots[i].transform.GetChild(0).GetComponent<carddata>();
                savedcardname.Add(cardinfo.slots[i].transform.GetChild(0).name);//取得该卡槽第一个子物体的名字添加到表里
                savedcardamount.Add(savedcardamount1.amount);//取得对应卡的数量
            }
        }
    }

    void cardtosave1con()
    {
        for (int i = 0; i < savedcardname.Count; i++)
        {
            cardtosave1.Add(new cardtosave1(savedcardname[i],savedcardamount[i]));
        }
    }



    public void test()
    {
        cardlisttosave();
        cardtosave1con();
        path = "/cards/" + input.text + ".json";
        savedcardtest = JsonMapper.ToJson(cardtosave1);
        if (File.Exists(input.text + ".json"))
        {
            File.Delete(input.text + ".json");
        }
        File.WriteAllText(Application.dataPath+path , savedcardtest.ToString());
        cardtosave1 = new List<cardtosave1>();
        savedcardname = new List<string>();
        savedcardamount = new List<int>();
        Menublack.SetActive(false);
        Savecardnametext.SetActive(false);
        Savecardname.SetActive(false);
        Savecardmenubtn.SetActive(false);

    }

    void Start()
    {
        Menublack = GameObject.Find("menublack");
        Savecardnametext = GameObject.Find("savecardnametext");
        Savecardname = GameObject.Find("savecardname");
        Savecardmenubtn = GameObject.Find("savecardmenubtn");
        cardinfo = GameObject.Find("cardmanager").GetComponent<cardcontrol>();//初始化
        
    }

}

public class cardtosave1
{
    public string name;
    public int amount; 

    public cardtosave1(string name, int amount)
    {
        this.name = name;
        this.amount = amount;
    }
}